package com.suixun.controller.credits;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.authority.controller.BaseManager;
import com.suixun.entity.form.credits.company.CompanyForm;
import com.suixun.entity.form.credits.company.CompanyPageForm;
import com.suixun.service.credits.CompanyService;
import com.suixun.service.credits.CompanyUserService;
import com.suixun.util.Constant;
import com.suixun.vo.credits.ResponseVo;

/**
 * 商家相关资料
 * 
 * @author yh
 * @date 2017年12月7日
 */
@RestController
@RequestMapping(value = "/credits/company")
public class CompanyController extends BaseManager {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CompanyUserService companyUserService;

    /**
     * 分页查询商家列表
     * 
     * @param param
     *            查询条件
     * @return
     */
    @RequestMapping(value = "/find_page.json", method = RequestMethod.GET)
    public Object findPage(CompanyPageForm param) {
        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verify()) {
                return fail(Constant.PARAM_ERROR);
            }
            return success(companyService.findPage(param));
        } catch (Exception e) {
            logger("CompanyController findPage:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 添加
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/insert.json", method = RequestMethod.POST)
    public Object insert(@RequestBody CompanyForm param) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verify()) {
                return fail(Constant.PARAM_ERROR);
            }
            boolean account = companyUserService.verifyAccount(param.getAccount(), null);
            boolean name = companyService.verifyName(param.getName(), null);
            if (!account) {
                return fail(Constant.People.PEOPLE_TEL_EXIST);
            }
            if (!name) {
                return fail("商户名称已存在");
            }
            companyService.insert(param, getUserId().intValue());
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            logger("CompanyController insert:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 修改
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public Object update(@RequestBody CompanyForm param) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verifyUpate()) {
                return fail(Constant.PARAM_ERROR);
            }
            boolean account = companyUserService.verifyAccount(param.getAccount(), param.getId());
            boolean name = companyService.verifyName(param.getName(), param.getId());
            if (!account || !name) {
                return fail(Constant.People.PEOPLE_TEL_EXIST);
            }
            companyService.update(param);
            return success();
        } catch (Exception e) {
            logger("CompanyController update:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 修改前的查询
     * 
     * @param companyid
     *            公司标识
     * @return
     */
    @RequestMapping(value = "/find_one_company.json", method = RequestMethod.GET)
    public Object findOneByCompanyid(@RequestParam("companyid") Integer companyid) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (companyid == null || companyid < 1) {
                return fail(Constant.PARAM_ERROR);
            }
            return success(new ResponseVo<>(companyService.findOneByCompanyid(companyid)));
        } catch (Exception e) {
            logger("CompanyController findOneByCompanyid:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 查询所有商户名称
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/find_all_name.json", method = RequestMethod.GET)
    public Object findAllName() {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            return success(new ResponseVo<>(companyService.findAllName()));
        } catch (Exception e) {
            logger("CompanyController findAllName:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 商家审核
     * 
     * @param opinion
     *            审核意见
     * @param isPass
     *            是否通过,true通过
     * @param companyid
     *            商户id
     * @return
     */
    @RequestMapping(value = "/audit_company.json", method = RequestMethod.POST)
    public Object auditCompany(@RequestParam("opinion") String opinion, @RequestParam("isPass") Boolean isPass,
                               @RequestParam("companyid") Integer companyid) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (companyid == null || StringUtils.isEmpty(opinion) || opinion.length() > 500 || isPass == null) {
                return fail(Constant.PARAM_ERROR);
            }
            companyService.auditCompany(companyid, opinion, isPass, getUserId().intValue());
            return success();
        } catch (Exception e) {
            logger("CompanyController auditCompany:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 验证商户名是否可用
     * 
     * @param name
     * @param companyid
     *            不为空则是修改时校验
     * @return
     */
    @RequestMapping(value = "/verify_name.json")
    public Object verifyName(@RequestParam(value = "name") String name,
                             @RequestParam(value = "companyid", required = false) Integer companyid) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (StringUtils.isEmpty(name)) {
                return fail("商户名已存在");
            }
            boolean bool = companyService.verifyName(name, companyid);
            return success(bool);
        } catch (Exception e) {
            logger("CompanyUserController verifyName:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }
    
    /**
     * 查询所有可用商户名称
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/find_all_pass_name.json", method = RequestMethod.GET)
    public Object findAllPassName() {
        
        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            return success(new ResponseVo<>(companyService.findAllPassName()));
        } catch (Exception e) {
            logger("CompanyController findAllPassName:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }
    
    /**
     * 置顶
     * 
     * @param id
     * @param isCancel
     *            是否取消置顶
     * @return
     */
    @RequestMapping(value = "/updateTop", method = RequestMethod.GET)
    public Object updateTop(@RequestParam(value = "id") Integer id, @RequestParam(value = "isCancel") boolean isCancel) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            companyService.updateTop(id, isCancel);
            return success();
        } catch (Exception e) {
            logger("CompanyController updateTop:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

}
